import { fail } from "assert";

/*
重庆时时彩 大小单双
*/
cc.Class({
    extends: cc.Component,

    properties: {
        tgShiList:cc.Node,
        tgGeList:cc.Node,
        sscPop:cc.Node,//根节点
        ndMiss:cc.Node,
        
        rules:[],//规则
        ruleMap:[],
        _curTopTitle:"大小单双",//当前玩法名
        _lotteryID:0,//彩种id
        _curGeSelect:-1,//个位选择球
        _curShiSelect:-1,//十位选择球
        _sscManage:null,
        
    },

    //重置初始值 -进来前调用
    initReset:function(){
    },

    showMiss:function(data){
        var childlen = this.ndMiss.children.length;
        var childs = this.ndMiss.children;
        var iChild = 0;

        for(var i=0;i<data.length;i++)
        {
            for(var j=0;j<data[i].length;j++)
            {
                if(iChild<childlen)
                {
                    childs[iChild].getComponent(cc.Label).string =data[i][j];
                    iChild++;
                }
            }
        }
        this.ndMiss.active = true;
    },

    hideMiss:function(){
        this.ndMiss.active = false;
    },

    // use this for initialization
    start: function () {
        this._sscManage = this.sscPop.getComponent(this.sscPop.name);
        if(this._sscManage == null)
        {
            ComponentsUtils.showTips("投注面板加载失败");
            return;
        }

        this.ruleMap = ["9","0","1","2"];//大小单双
        this.rules = DEFINE.LOTTERYRULESSC.BXDS;
        this._lotteryID = DEFINE.LOTTERY_ID.SSC_CQ;

        var shiChilds = this.tgShiList.children;
        var geChilds = this.tgGeList.children;
        for(var i=0;i<shiChilds.length;i++)
        {
            WidgetExtUtils.addCheckEvent(shiChilds[i],this.node,this.node.name,"onShiClick", this.ruleMap[i]);
        }
        for(var i=0;i<geChilds.length;i++)
        {
            WidgetExtUtils.addCheckEvent(geChilds[i],this.node,this.node.name,"onGeClick", this.ruleMap[i]);
        }
    },

    onGeClick:function(toggle, customEventData){
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this._curGeSelect = customEventData;
        }
        else
        {
            this._curGeSelect = -1;
        }

        this.updateTgSelect();
        this.checkBet();
    },

    onShiClick:function(toggle, customEventData){
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this._curShiSelect = customEventData;
        }
        else
        {
            this._curShiSelect = -1;
        }

        this.updateTgSelect();
        this.checkBet();
    },

    //检查倍数
    checkBet:function(){
        var bet = 0;
        if(this._curShiSelect != -1 && this._curGeSelect != -1)
        {
            bet = 1;
        }

        this._sscManage.showAmount(bet);
    },

    updateTgSelect:function(){
        if(this._curShiSelect > -1 || this._curGeSelect > -1)
        {
            window.Notification.emit("updateTgSelect",false);
        }
        else
        {
            window.Notification.emit("updateTgSelect",true);
        }
    },

     //投注信息组合
     dataObj:function(){
        //组合
        var obj = new Object(); 
        obj.PlayCode = parseInt(DEFINE.LOTTERY_ID.SSC_CQ + this.rules); 
        var nums = "";
        nums = this._curShiSelect + "|" + this._curGeSelect;
        obj.num = nums;
        return obj;
    },

     //清空当前选择
     clearAllBetRecord:function(){
        this._curGeSelect = -1;
        this._curShiSelect = -1;
        for(var i=0;i<this.tgShiList.children.length;i++)
        {
            this.tgShiList.children[i].getComponent(cc.Toggle).isChecked = false;
            this.tgGeList.children[i].getComponent(cc.Toggle).isChecked = false;
        }
    },

    //机选
    randomSelectCallBack:function(){
        this.clearAllBetRecord();
        var randomArray = Utils.getRandomArrayWithArray(this.tgGeList.children, 1);
        if(randomArray.length>0)
        {
            randomArray[0].getComponent(cc.Toggle).isChecked = true;
            var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
            this.onGeClick(randomArray[0],temp);
        }
        randomArray = Utils.getRandomArrayWithArray(this.tgShiList.children, 1);
        if(randomArray.length>0)
        {
            randomArray[0].getComponent(cc.Toggle).isChecked = true;
            var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
            this.onShiClick(randomArray[0],temp);
        }
        return true;
    },

});
